﻿using Api.BLL;
using Api.Common;
using Api.Model;
using Api.Web.filter;
using Api.Web.helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Api.Web.Areas.Admin.Controllers
{
    [CheckAdminProject]
    public class DemandController : AdminController
    {
        public ActionResult Index()
        {
            var model = ProblemBLL.Current.GetModel(s => s.ProjectId == projectId && s.UserId == userId && s.Type == "Demand");
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Index([Bind(Include = "Id,Description")] Problem problem)
        {
            bool flag = true;
            problem.Type = "Demand";
            if (problem.Id > 0)
            {
                try
                {
                    ProblemBLL.Current.UpdateIgnore(s => new { s.CreateDate, s.UserId, s.ProjectId }, problem);
                }
                catch
                {
                    flag = false;
                }
            }
            else
            {
                try
                {
                    problem.ProjectId = projectId;
                    problem.UserId = userId;
                    problem.CreateDate = DateTime.Now;
                    ProblemBLL.Current.Add(problem);
                }
                catch
                {
                    flag = false;
                }
            }

            if (flag)
            {
                AjaxMsgHelper.ResponseSuccess("操作成功", Url.Action("index", new { Area = "Admin" }));
            }
            else
            {
                AjaxMsgHelper.ResponseFail("操作失敗，請重試");
            }
        }
    }
}